package com.ksfzhaohui.nio.buffer;

import java.nio.CharBuffer;

public class BuffFillDrain {

	public static int index = 0;

	public static String strings[] = { "A random String value",
			"Help Me! Help Me!" };

	private static boolean fillBuffer(CharBuffer buffer) {
		if (index >= strings.length) {
			return false;
		}

		String string = strings[index];
		for (int i = 0; i < string.length(); i++) {
			buffer.put(string.charAt(i));
		}

		return true;
	}

	private static void drainBuffer(CharBuffer buffer) {
		while (buffer.hasRemaining()) {
			System.out.print(buffer.get());
		}

		System.out.println("");
	}

	public static void main(String[] args) {
		CharBuffer buffer = CharBuffer.allocate(100);
		while (fillBuffer(buffer)) {
			buffer.flip();
			drainBuffer(buffer);
			buffer.clear();
			index++;
		}
	}
}
